package com.erci.controller.user;


import com.erci.entity.DTO.LoginDTO;
import com.erci.entity.DTO.LoginPhoneDTO;
import com.erci.entity.DTO.UserUserUpdateDTO;
import com.erci.entity.VO.UserPhoneVO;
import com.erci.entity.VO.UserRegisterVO;
import com.erci.entity.VO.UserUserVO;
import com.erci.result.Result;
import com.erci.service.UserService;
import com.erci.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

//@CrossOrigin(origins = "http://localhost:5172")
@RestController("userUserController")
@RequestMapping("/user/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/login")
    public Result<UserUserVO> loginByPassword(@RequestBody LoginDTO loginDTO) {
        UserUserVO userUserVO = userService.loginByPassword(loginDTO);
        return Result.success(userUserVO);
    }
    @GetMapping("/sendSmsCode")
    public Result< String> sendSmsCode(@RequestParam("phone") String phone) {
        return userService.sendSmsCode(phone);
    }

    @PostMapping("/smsLogin")
    public Result<UserPhoneVO> smsLogin(@RequestBody LoginPhoneDTO loginPhone){
        String phone = loginPhone.getPhone();
        String code = loginPhone.getCode();
        return userService.smsLogin(phone,code);
    }

    @PostMapping("/register")
    public Result<UserRegisterVO> register(@RequestBody LoginDTO loginDTO) {
        return userService.register(loginDTO);
    }

    ////////////////////////////////////////////////////////////
    @PostMapping("/uploadAvatar")
    public Result<Map<String, String>> upload(@RequestParam("file") MultipartFile file, @RequestParam("userId") Integer userId) {
        log.info("图片上传 ");
        try {
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uuid = userId.toString();
            String objectName = "anime_Project_" + uuid + suffix;
            //上传文件
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(Map.of("url", filePath));
        } catch (IOException e) {
            log.info("上传失败");
        }
        return Result.error("上传失败");
    }
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserUserUpdateDTO userUserVO) {
        userService.userUpdate(userUserVO);
        return Result.success("修改成功");
    }
    //绑定手机号验证
    @PostMapping("/bindPhone")
    public Result<String> bindPhone(@RequestBody LoginPhoneDTO loginPhoneDTO) {

        return userService.bindPhone(loginPhoneDTO);
    }
    //解绑手机号
    @PostMapping("/unbindPhone")
    public Result<String> unbindPhone(@RequestBody LoginPhoneDTO loginPhoneDTO) {
        return userService.unbindPhone(loginPhoneDTO);
    }

}
